package org.chnxi.study.messagequeue.rockermq.producer;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

/**
 * 发送异步消息，发送线程不关心发送结果，发送结果将使用SendCallback回调的方式处理
 */
public class AsyncProducer {

    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException {
        DefaultMQProducer producer = new DefaultMQProducer();
        producer.setNamesrvAddr("192.168.50.227:9876;192.168.50.227:9876");
        producer.start();


        for (int i=0; i<10; i++){
            Message message = new Message("topicName", "tag", (i + "asyncMessageSend").getBytes());

            producer.send(message, new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    System.out.println("发送成功处理函数：");
                    SendStatus status = sendResult.getSendStatus(); //获取发送结果
                    sendResult.getMsgId(); //Msg的ID
                    sendResult.getMessageQueue().getQueueId();//queueId
                }

                @Override
                public void onException(Throwable throwable) {
                    System.out.println("发送失败处理函数：");
                    System.out.println("Msg="+message.toString());
                    System.out.println("ExceptionMsg="+throwable.getMessage());
                }
            });
        }
    }

}
